package cn.com.hatech.cdkey.module.index.service.impl;

import cn.com.hatech.cdkey.common.constant.WebConst;
import cn.com.hatech.cdkey.module.index.entity.Log;
import cn.com.hatech.cdkey.module.index.mapper.LogMapper;
import cn.com.hatech.cdkey.module.index.service.ILogService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * @projectName: hatech-blog
 * @package: cn.com.hatech.blog.module.admin.service.impl
 * @className: LogServiceImpl
 * @author: xuruikai
 * @description: 日志信息业务层接口实现
 * @date: 2020/1/11 14:27
 * @version: 1.0
 */
@Slf4j
@Service("logService")
public class LogServiceImpl implements ILogService {

    @Resource
    private LogMapper logMapper;

    @Override
    public int insertLog(String action, String data, String ip, String authorId) {
        Log logs = new Log();
        logs.setAction(action);
        logs.setData(data);
        logs.setIp(ip);
        logs.setAuthorId(authorId);
        logs.setCreateTime(new Date());
        return logMapper.insert(logs);
    }

    @Override
    public List<Log> getLogs(int page, int limit) {
        log.debug("Enter getLogs method:page={},limit={}", page, limit);
        if (page <= 0) {
            page = 1;
        }
        if (limit < 1 || limit > WebConst.MAX_POSTS) {
            limit = 10;
        }
        QueryWrapper<Log> logQueryWrapper = new QueryWrapper<>();
        logQueryWrapper.orderByDesc("create_time");
        IPage<Log> logVos = logMapper.selectPage(new Page<>(page, limit), logQueryWrapper);
        log.debug("Exit getLogs method");
        return logVos.getRecords();
    }
}
